Shader "Unlit/DiffuseOutputWithSettings" // 重命名 Shader 以反映其功能
{
    Properties
    {
        [MainTexture] _BaseMap("Base Map (RGB)", 2D) = "white" {} // 添加 Diffuse 贴图属性
        [MainColor] _BaseColor("Base Color", Color) = (1,1,1,1) // 添加基础颜色属性

        // --- Surface Options ---
        [Header(Surface Options)]
        [Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull Mode", Float) = 2 // Back
        [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Src Blend", Float) = 1 // One
        [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Dst Blend", Float) = 0 // Zero
        [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("Blend Operation", Float) = 0 // Add
        [Toggle] _ZWrite("ZWrite", Float) = 1
        [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("ZTest", Float) = 4 // LEqual

        // --- Stencil Settings ---
        [Header(Stencil Settings)]
        _StencilRef("Stencil Ref", Float) = 0
        [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Comp", Float) = 8 // Always
        [Enum(UnityEngine.Rendering.StencilOp)] _StencilPass("Stencil Pass", Float) = 0 // Keep
        [Enum(UnityEngine.Rendering.StencilOp)] _StencilFail("Stencil Fail", Float) = 0 // Keep
        [Enum(UnityEngine.Rendering.StencilOp)] _StencilZFail("Stencil ZFail", Float) = 0 // Keep
        [IntRange] _StencilReadMask("Read Mask", Range(0, 255)) = 255
        [IntRange] _StencilWriteMask("Write Mask", Range(0, 255)) = 255

        // --- Overlay Settings ---
        [Header(Overlay Settings)]
        [Toggle] _DrawOverlay("Enable Overlay", Float) = 0
        [Enum(UnityEngine.Rendering.BlendMode)] _OverlaySrcBlend("Overlay Src Blend", Float) = 1 // One
        [Enum(UnityEngine.Rendering.BlendMode)] _OverlayDstBlend("Overlay Dst Blend", Float) = 0 // Zero
        [Enum(UnityEngine.Rendering.BlendOp)] _OverlayBlendOp("Overlay Blend Op", Float) = 0 // Add
        _OverlayStencilRef("Overlay Stencil Ref", Float) = 0
        [Enum(UnityEngine.Rendering.CompareFunction)] _OverlayStencilComp("Overlay Stencil Comp", Float) = 8 // Always
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
        LOD 100

        // --- 主Pass ---
        Pass
        {
            Name "DiffuseOutput" // 重命名 Pass
            Tags { "LightMode" = "SRPDefaultUnlit" }

            // 应用Surface Options设置
            Cull [_Cull]
            Blend [_SrcBlend] [_DstBlend]
            BlendOp [_BlendOp]
            ZWrite [_ZWrite]
            ZTest [_ZTest]

            // 应用Stencil设置
            Stencil
            {
                Ref [_StencilRef]
                Comp [_StencilComp]
                Pass [_StencilPass]
                Fail [_StencilFail]
                ZFail [_StencilZFail]
                ReadMask [_StencilReadMask]
                WriteMask [_StencilWriteMask]
            }

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // 移除了 #pragma shader_feature _DRAWOVERLAY_ON

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            // 声明纹理和颜色变量
            TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
            CBUFFER_START(UnityPerMaterial)
            float4 _BaseMap_ST;
            float4 _BaseColor;
            CBUFFER_END

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 texcoord : TEXCOORD0; // 添加 UV 坐标
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv : TEXCOORD0; // 传递 UV 坐标
            };

            Varyings vert (Attributes v)
            {
                Varyings o;
                o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
                o.uv = TRANSFORM_TEX(v.texcoord, _BaseMap); // 计算 UV
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                // 采样 Diffuse 贴图并乘以基础颜色
                half4 col = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv) * _BaseColor;
                return col;
            }
            ENDHLSL
        }

        // // --- Overlay Pass (保持不变) ---
        // Pass
        // {
        //     Name "Overlay"
        //     Tags { "LightMode" = "UniversalForward" }

        //     // 覆盖模式特殊设置
        //     Blend [_OverlaySrcBlend] [_OverlayDstBlend]
        //     BlendOp [_OverlayBlendOp]
        //     ZTest Always
        //     ZWrite Off
        //     Cull Off

        //     Stencil
        //     {
        //         Ref [_OverlayStencilRef]
        //         Comp [_OverlayStencilComp]
        //         Pass Keep
        //     }

        //     HLSLPROGRAM
        //     #pragma vertex vert
        //     #pragma fragment frag
        //     #pragma multi_compile_instancing

        //     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

        //     struct Attributes
        //     {
        //         float4 positionOS : POSITION;
        //         UNITY_VERTEX_INPUT_INSTANCE_ID
        //     };

        //     struct Varyings
        //     {
        //         float4 positionCS : SV_POSITION;
        //         UNITY_VERTEX_INPUT_INSTANCE_ID
        //     };

        //     Varyings vert (Attributes v)
        //     {
        //         Varyings o;
        //         UNITY_SETUP_INSTANCE_ID(v);
        //         UNITY_TRANSFER_INSTANCE_ID(v, o);
        //         o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
        //         return o;
        //     }

        //     half4 frag (Varyings i) : SV_Target
        //     {
        //         UNITY_SETUP_INSTANCE_ID(i);
        //         return half4(0,1,0,0.3); // 绿色半透明覆盖层
        //     }
        //     ENDHLSL
        // }
    }

    CustomEditor "UnityEditor.ShaderGraph.PBRMasterGUI" // 如果不需要 Shader Graph 编辑器，可以移除这行
}